import { useDispatch, useSelector } from 'react-redux'
import Icon from '../../../../components/Icon'
import styles from './index.module.scss'
import { differenceBy } from 'lodash'
import classNames from 'classnames'
import { useState } from 'react'
import { addChannel, delChannel } from '../../../../store/action/home'
import { Toast } from 'antd-mobile'
// import { differenceBy } from 'lodash/differenceBy'

/**
 * 频道管理组件
 * @param {Number} props.tabActiveIndex 用户选中的频道的索引
 * @param {Function} props.onClose 关闭频道管理抽屉时的回调函数
 * @param {Function} props.onChannelClick 当点击频道列表中的某个频道时的会带哦函数
 */
const Channels = ({
  tabActiveIndex,
  onClose,
  onChannelClick,
  index,
  onChange,
}) => {
  const dispatch = useDispatch()
  // 拿到用户频道
  const userChannels = useSelector((state) => state.home.userChannels)
  // 拿到推荐频道
  const recommendChannels = useSelector((state) => {
    const { userChannels, allChannels } = state.home
    return differenceBy(allChannels, userChannels, 'id') //注意 这里的数组前后顺序有影响
    // return allChannels.filter(
    // 筛选所有频道 去除里面的用户频道 得到推荐频道
    // (item) => userChannels.findIndex((v) => v.id === item.id) === -1
    // 如果我这个频道在userchannels中 就不要
    // )
  })
  // console.log(recommendChannels)
  // 切换
  const changeChannel = (i) => {
    if (editing) return

    onChange(i)
    onClose()
  }
  //  处理编辑状态
  const [editing, setEditing] = useState(false)

  // 删除频道
  const del = (channel, i) => {
    // console.log(channel)
    if (userChannels.length <= 4) {
      Toast.show('至少保留4个频道了啦')
      return
    }
    dispatch(delChannel(channel))
    // 高亮处理
    // 1 如果删除的i 和 index相等，默认让推荐0 高亮，
    // 2 如果删除的i 小于 index  默认让index-1高亮
    if (i === index) {
      onChange(0)
    } else if (i < index) {
      onChange(index - 1)
    }
  }
  const add = (channel) => {
    // console.log(channel)
    dispatch(addChannel(channel))
    Toast.show('添加成功',1)
  }
  return (
    <div className={styles.root}>
      {/* 顶部栏：带关闭按钮 */}
      <div className="channel-header">
        <Icon type="iconbtn_channel_close" onClick={onClose} />
      </div>

      {/* 频道列表 */}
      <div className="channel-content">
        {/* 当前已选择的频道列表 */}
        <div
          className={classNames('channel-item', {
            edit: editing,
          })}
        >
          <div className="channel-item-header">
            <span className="channel-item-title">我的频道</span>
            <span
              className="channel-item-title-extra"
              onClick={() => setEditing(!editing)}
            >
              {editing ? '点击删除频道' : '点击进入频道'}
            </span>
            <span
              className="channel-item-edit"
              onClick={() => setEditing(!editing)}
            >
              {editing ? '完成' : '编辑'}
            </span>
          </div>

          <div className="channel-list">
            {userChannels.map((item, i) => (
              <span
                className={classNames('channel-list-item', {
                  selected: index === i,
                })}
                key={item.id}
                onClick={() => changeChannel(i)}
              >
                {item.name}
                {item.id !== 0 && (
                  // 推荐不允许删除
                  <Icon type="icon-close" onClick={() => del(item, i)} />
                )}
              </span>
            ))}
          </div>
        </div>

        {/* 推荐的频道列表 */}
        <div className="channel-item">
          <div className="channel-item-header">
            <span className="channel-item-title">频道推荐</span>
            <span className="channel-item-title-extra">点击添加频道</span>
          </div>
          <div className="channel-list">
            {recommendChannels.map((item) => (
              <span
                key={item.id}
                className="channel-list-item"
                onClick={() => add(item)}
              >
                {item.name}
              </span>
            ))}
          </div>
        </div>
      </div>
    </div>
  )
}

export default Channels
